package com.hr.utils;

import freemarker.template.Configuration;
import freemarker.template.Template;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;

@Service
public class ResumeUtils {

    Configuration FMConfig = FreeMarkerUtils.createConfig();

    public boolean createAutoCode(Object obj) {
        try {
            Template modelTemplate = FMConfig.getTemplate("resume.ftl");
            generate(modelTemplate, obj,  "/Users/aaa/Documents/resume/",  obj.toString() + ".md");
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

    private void generate(Template template, Object obj, String path, String fileName) {

        String filePath = path + fileName;

        try {
            File dir = new File(path);

            if(!dir.exists()) {
                dir.mkdirs();
            }

            OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream(filePath));
            template.process(obj, osw);

        } catch (Exception ex) {
            System.out.println("can not create " + filePath);
            ex.printStackTrace();
        } finally {
            System.out.println("create " + filePath);
        }

    }
}
